/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.chain.shell;

import tal.drivechain.chain.Chain;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.chain.NodeExport;
import tal.drivechain.chain.script.ScriptCommand;

/**
 *
 * @author eisental
 */
public class NodeExportObject extends ShellObject {
    public NodeExportObject(DriveShell shell) {
        super("nodeExport", "exports", "export", shell);
    }

    @Override
    public boolean isActionSupported(Action action) {
        return (action!=Action.set);
    }

    @Override
    public void make(String command) throws IllegalArgumentException {
        // make export mod [{props}];
        Chain c = shell.getActiveChain();
        NodeExport n = NodeExport.parseModuleExport(ScriptCommand.parseScriptCommand("export " + command), shell.getActiveChain());
        c.addNodeExport(n);
        out.println("Exporting node properties: \n" + InfoPrinter.printNodeExport(c, n.getExportedNode()));
    }

    @Override
    public void remove(String command) throws IllegalArgumentException {
        // remove export of <mod-instance>
        if (command.startsWith("of "))
            command = command.substring("of ".length()).trim();
        ChainNode node = shell.getActiveChain().getChildNodes().get(command);
        if (node==null) throw new IllegalArgumentException("Node " + command + " does not exist in chain " + shell.getActiveChain().getName());

        NodeExport e = shell.getActiveChain().getNodeExports().get(node);
        shell.getActiveChain().removeNodeExport(node);
        out.println("Unexported properties of node " + command + ".");
    }

    @Override
    public void set(String command) throws IllegalArgumentException {

    }

    @Override
    public void list() throws IllegalArgumentException {
        out.println(InfoPrinter.printNodeExports(shell.getActiveChain()));
    }

    @Override
    public void inspect(String command) throws IllegalArgumentException {
        //inspect export of <mod instance>
        if (command.startsWith("of "))
            command = command.substring(3).trim();
        ChainNode node = shell.getActiveChain().getChild(command);
        if (node==null) throw new IllegalArgumentException("Node " + command + " does not exist in chain " + shell.getActiveChain().getName());
        out.println(InfoPrinter.printNodeExport(shell.getActiveChain(), node));
    }

}
